#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#include "variables.h"


//«Jan-1 Jun-6 Jul-7» «Mar-3 May-5» «Apr-4 Aug-8» Feb-2 Sep-9 Nov-10 Oct-11 Dec-12
int month_detect (char * found)
{
	if (found[0]=='J')
	{
		if (found[1]=='a')
		{
			return 1;
		}
		else if (found[2]=='n')
		{
			return 6;
		}
		else
		{
			return 7;
		}
	}
	else if (found[0]=='M')
	{
		if (found[2]=='r')
		{
			return 3;
		}
		else
		{
			return 5;
		}
	}
	else if (found[0]=='A')
	{
		if (found[1]=='p')
		{
			return 4;
		}
		else
		{
			return 8;
		}
	}
	else if (found[0]=='F')
	{
		return 2;
	}
	else if (found[0]=='S')
	{
		return 9;
	}
	else if (found[0]=='N')
	{
		return 10;
	}
	else if (found[0]=='O')
	{
		return 11;
	}
	else if (found[0]=='D')
	{
		return 12;
	}
	else
	{
		return 0;
	}
}	

char * date_seek(char * found)
{
	int cpt=0;
	while (found[cpt]!=' ')
	{
		found++;
	}
	found++;
	return found;
}

void hours_retrieval(hours * time, char * ptr)
{
	time->hrs=atoi(ptr);
	ptr=ptr+3;
	time->min=atoi(ptr);
	ptr=ptr+3;
	time->sec=atoi(ptr);
}

void time_affiche (hours * ptr)
{
	printf ("***************TIME*************\n");
	printf("le mois    :%i\n", ptr->month);
	printf("le Jour    :%i\n", ptr->day);
	printf("l'heure    :%i\n", ptr->hrs);
	printf("la minute  :%i\n", ptr->min);
	printf("la seconde :%i\n", ptr->sec);
	printf("l'année    :%i\n", ptr->year);
	printf ("***************END**************\n\n");
}

//le paramettre est de type : «Thu Nov 19 21:04:13 2009»	 
hours * time_read (char * buffer)
{
	char * found;
	hours * time=(hours*)malloc(sizeof(hours));
	found=strstr(buffer, "<");
	if (found!=NULL)
	{
		found=date_seek(found);
		time->month=month_detect(found);
		found=date_seek(found);
		time->day=atoi(found);
		found=date_seek(found);
		hours_retrieval(time, found);
		found=date_seek(found);
		time->year=atoi(found);
	}
	time_affiche(time);
	return time;
	free(time);
}


void affiche_syear(s_years * ptr)
{
	printf ("************Structure S_years***************\n");
	printf ("l'annee courante = %i \n", ptr->current_year);
	printf ("le nombre d'Annee Bi = %i \n", ptr->nb_leap_year);
}


int spotting_leap(int a)
{
	if((((a%4)==0 && (a%100)!=0)) || (a%400)==0 )
	{
		//printf("cette année=%i est bissextile\n",a);
		return 1;
	}
	else
		return 0;
}

s_years annees_bissextiles(int annee_courante)
{
	s_years var;
	var.current_year=0;
	var.nb_leap_year=0;
	//affiche_syear(&var);
	int cpt=0;
	int a=0;
	for(a=1970;a<annee_courante;a++)
	{
		if(spotting_leap(a)==1)
		{
			cpt=cpt+1;
		}
	}

	var.nb_leap_year=cpt;
	//printf("cpt%i\n",cpt);
	var.current_year=spotting_leap(annee_courante);
	//affiche_syear(&var);
	return var;
}

int recognition_month(int a,int b)
{
	int mois=0;
	switch(b)
	{
		case 1:
			break;
		case 2:
			mois=(31*24*60*60);
			break;
		case 3:
			mois=(31+a)*(24*60*60);
			break;
		case 4:
			mois=(31+a+31)*(24*60*60);
			break;
		case 5:
			mois=(31+a+31+30)*(24*60*60);
			break;
		case 6:
			mois=(31+a+31+30+31)*(24*60*60);
			break;
		case 7:
			mois=(31+a+31+30+31+30)*(24*60*60);
			break;
		case 8:
			mois=(31+a+31+30+31+30+31)*(24*60*60);
			break;
		case 9:
			mois=(31+a+31+30+31+30+31+31)*(24*60*60);
			break;
		case 10:
			mois=(31+a+31+30+31+30+31+31+30)*(24*60*60);
			break;
		case 11:
			mois=(31+a+31+30+31+30+31+31+30+31)*(24*60*60);
			break;
		case 12:
			mois=(31+a+31+30+31+30+31+31+30+31+30)*(24*60*60);
			break;
		default:
			mois=-1;
	//		printf("nari nari kebetiha\n");
			break;
	}
	//printf("b est %i et a est %i et mois est %i\n",b,a,mois);
	return mois;

}	


int converter(hours *time, s_years* var)
{
	int years=0;
	years=(time->year)-1970;

/*	printf ("\n*********************\n");
	printf("'année %i\n",time->year);
    	printf ("La Différence d'Annee = %i\n", years);
*/
	years=((years*365)+var->nb_leap_year)*24*60*60;

/*	printf ("Après Converstion en Seconde = %i\n", years);
	printf ("*********************\n\n");
*/
	//on a transformé ttes les années depuis 1970 en secondes en rajoutant les jours manquant des années bissextiles
	int mois=0;
	if(var->current_year==1)
		{
			mois=recognition_month(29, time->month);
//			printf ("Le mois converti en Seconde = %i\n", mois);
		}
	else
		{
		mois=recognition_month(28,time->month);
//		printf ("Le mois converti en Seconde = %i\n", mois);

		}
	int days=0;
	days=(time->day-1)*24*60*60;

/*	printf ("********************\n");
	printf ("Les Jours converti en Seconde = %i\n", days);
	printf ("********************\n");
*/
	int seconde=0;
	seconde=years+mois+days+(time->hrs*3600)+(time->min*60)+time->sec;
	return seconde;
}

int difference_now(hours *h)
{
	s_years p;
	p=annees_bissextiles(h->year);
//	affiche_syear(&p);
	int i=0;
	i=time(NULL);
	int resultat=0;
	resultat=converter(h,&p);
	//printf("le resultat est %i \n",resultat);
	//printf("la date  est %i \n",i);
	resultat=i-resultat;
	//printf("le la difference de resultat est %i \n",resultat);
	
/*
	int e=0;
	e=(((resultat/60)/60)/24/30);
	printf("le nbre de mois est %i \n",e);
*/
	return resultat;
	free(h);
}
